import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import adminRouter from './modules/admin'



/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  // 忘记密码的路由
  {
    hidden: true,
    path: '/retrievePassword',
    name: 'retrievePassword',
    component: () => import('@/views/retrievePassword/retrievePassword')
  },

  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },

  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'el-icon-s-home', affix: true }
      }
    ]
  },

  {
    path: '/showData',
    component: Layout,
    redirect: '/common/showData',
    // hidden: true,
    children: [
      {
        path: 'showData',
        component: () => import('@/views/common/showData'),
        name: 'showData',
        meta: { title: 'showData', icon: 'dashboard', noCache: true }
      }
    ]
  },

  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    // hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'profile', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/chatRoom',
    component: Layout,
    redirect: '/chatRoom/index',
    // hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/common/chatRoom'),
        name: 'chatRoom',
        meta: { title: 'chatRoom', icon: 'el-icon-s-comment', noCache: true }
      }
    ]
  },
  

]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [


  // {
  //   path: '/icon',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/icons/index'),
  //       name: 'Icons',
  //       meta: { title: 'icons', icon: 'icon', noCache: true }
  //     }
  //   ]
  // },

  /** when your routing map is too long, you can split it into small modules **/
  /** 有太多的子路由可以填写到一个路由里统一管理  **/




  /* ------------------------- 管理员和教师共同的路由---------------------- */
  {
    path: '/common',
    component: Layout,
    redirect: 'common/noticeSend',
    children: [
      {
        path: 'noticeSend',
        component: () => import('@/views/common/noticeSend'),
        name: 'noticeSend',
        meta: { title: 'noticeSend', icon: 'el-icon-message-solid', roles: ['admin', 'teacher'] }
      }
    ]
  },

  /* ------------------------- 管理员和教师共同的路由---------------------- */



  /* ------------------------- 教师的路由---------------------- */
  {
    path: '/teacher',
    component: Layout,
    redirect: 'teacher/leaveApproval',
    children: [
      {
        path: 'leaveApproval',
        component: () => import('@/views/teacher/leaveApproval'),
        name: 'leaveApproval',
        meta: { title: 'leaveApproval', icon: 'el-icon-thumb', roles: ['teacher'] }
      }
    ]
  },
  {
    path: '/teacherManage',
    component: Layout,
    redirect: '/teacherManage/studentMessage',
    meta: {
      title: 'studentMessage',
      icon: 'table',
      roles: ['teacher']
    }, children: [
      {
        path: 'studentMessage',
        component: () => import('@/views/teacher/studentMessage'),
        name: 'studentMessage',
        meta: { title: 'studentAdmin', icon: 'el-icon-user-solid' }

      }]
  },
  /* ------------------------- 教师的路由---------------------- */





  /* -------------------------管理员的路由---------------------- */
  {
    path: '/email',
    component: Layout,
    redirect: 'email/emailToSend',
    meta: {
      title: 'emailToSend',
      icon: 'el-icon-message',
      roles: ['admin']
    },
    children: [
      {
        path: 'emailToSend',
        component: () => import('@/views/admin/emailToSend'),
        name: 'emailToSend',
        meta: { title: 'emailToSend' }
      }
    ]
  },

  adminRouter,

  {
    path: '/log',
    component: Layout,
    redirect: 'log/logManage',
    meta: {
      title: 'log',
      icon: 'el-icon-reading',
      roles: ['admin'],
      noCache: true
    },
    children: [
      {
        path: 'logManage',
        component: () => import('@/views/admin/log'),
        name: 'log',
        meta: { title: 'log' }
      }
    ]
  },

  {
    path: '/error-log',
    component: Layout,
    children: [
      {
        path: 'log',
        component: () => import('@/views/error-log/index'),
        name: 'ErrorLog',
        meta: { title: 'errorLog', icon: 'bug', roles: ['admin'] }
      }
    ]
  },
  /* -------------------------管理员的路由---------------------- */



  /* -------------------------学生的路由---------------------- */
  {
    path: '/studentNotice',
    component: Layout,
    redirect: 'student/myNotice',
    meta: {
      title: 'myNotice',
      icon: 'el-icon-message-solid',
      roles: ['student']
    },
    children: [
      {
        path: 'myNotice',
        component: () => import('@/views/student/myNotice'),
        name: 'myNotice',
        meta: { title: 'myNotice' }
      }
    ]
  },
  {
    path: '/studentApplication',
    component: Layout,
    redirect: 'student/application',
    meta: {
      title: 'application',
      icon: 'el-icon-edit-outline',
      roles: ['student']
    },
    children: [
      {
        path: 'application',
        component: () => import('@/views/student/application'),
        name: 'application',
        meta: { title: 'application' }
      }
    ]
  },
  {
    path: '/studentHealthTable',
    component: Layout,
    redirect: 'student/healthTable',
    meta: {
      title: 'healthTable',
      icon: 'el-icon-position',
      roles: ['student']
    },
    children: [
      {
        path: 'healthTable',
        component: () => import('@/views/student/healthTable'),
        name: 'healthTable',
        meta: { title: 'healthTable' }
      }
    ]
  },
  /* -------------------------学生的路由---------------------- */




  // 以上路由匹配不到的时候 会返回主页 或者可以设置404
  { path: '*', redirect: '/dashboard', hidden: true },
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
